/**
 * @class   wosh::gui::WidgetMediaZone
 * @brief   
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: WidgetMediaZone.h 2748 2010-07-11 22:07:06Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_WidgetMediaZone_H__
 #define __WOSH_Gui_WidgetMediaZone_H__

 #include <core/URI.h>
 #include <core/UserManager.h>
 #include <core/MethodsCommon.h>
 #include <core/Request.h>
 #include <core/Response.h>
 #include <core/Table.h>

 #include <framework/entertainment/MediaZone.h>

 #include <framework/gui/IWidget.h>
 #include <framework/gui/WoshTableModel.h>

 #include "ui_WidgetMediaZone.h"
 #include <QItemDelegate>
 #include <QMenu>
 #include <QMessageBox>


namespace wosh {
 namespace gui {

class DeviceMappingActionsDelegage : public QItemDelegate  {

	Q_OBJECT

	public:
		DeviceMappingActionsDelegage(QObject *parent = 0) : QItemDelegate(parent) { }
		virtual ~DeviceMappingActionsDelegage() { }

		QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
		 {
			 //index.column() == 1
			 if ( index.data().type() == QVariant::Int ) {
				QComboBox* cmbEditor = new QComboBox(parent);
				cmbEditor->addItem("UNKNOWN");
				cmbEditor->addItem("PLAY");
				cmbEditor->addItem("TRACK");
				cmbEditor->addItem("VOLUME");
				return cmbEditor;
			 }
			return QItemDelegate::createEditor(parent, option, index);
		 }

		void setEditorData(QWidget *editor, const QModelIndex &index) const
		 {
			if ( index.data().type() == QVariant::Int ) {
				QComboBox* cmbEditor = static_cast<QComboBox*>(editor);
				int value = index.model()->data(index, Qt::EditRole).toInt();
				switch(value) {
					case 1:	cmbEditor->setCurrentIndex(1); break;
					case 2:	cmbEditor->setCurrentIndex(2); break;
					case 3:	cmbEditor->setCurrentIndex(3); break;
					case 0:
					default: cmbEditor->setCurrentIndex(0); break;
				 }
			 }
			else
				QItemDelegate::setEditorData(editor, index);
		 }

		void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
		 {
			if ( index.data().type() == QVariant::Int ) {
				QComboBox* cmbEditor = static_cast<QComboBox*>(editor);
				int value = 0;
				switch(cmbEditor->currentIndex()) {
					case 1:	value = 1; break;
					case 2:	value = 2; break;
					case 3:	value = 4; break;
					case 0:
					default: value = 0; break;
				 }
				model->setData(index, value, Qt::EditRole);
			 }
			else
				QItemDelegate::setModelData(editor, model, index);
		 }

		void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
		 {
			(void)index;
			editor->setGeometry(option.rect);
		 }

 };


class WidgetMediaZone :  public QWidget, public IWidget  {

	Q_OBJECT

	public:

		WidgetMediaZone(QWidget *parent = 0) : QWidget(parent), IWidget()
		 {
			this->m_ui = new Ui::WidgetMediaZone();
			this->m_ui->setupUi(this);

			this->setWindowTitle("MediaZone");
			this->getWidgetURI().setName("WidgetMediaZone#" + Utilities::randomLong() );

			buildMenus();

			this->propMapper = new PropertiesMapper(this);
			this->propMapper->getMap().insert(_KEY_name, this->m_ui->lblName);
			this->propMapper->getMap().insert(_MediaZone_KEY_ZoneType, this->m_ui->cmbZoneType);
			this->propMapper->getMap().insert(_MediaZone_KEY_ZoneRole, this->m_ui->cmbZoneRole);

			this->propMapper->getMap().insert(_MediaZone_KEY_VolumeMin, this->m_ui->sliderVolumeMin);
			this->propMapper->getMap().insert(_MediaZone_KEY_VolumeMax, this->m_ui->sliderVolumeMax);
			this->propMapper->getMap().insert(_MediaZone_KEY_VolumeDefault, this->m_ui->sliderVolumeDefault);

//			this->m_ui->tableViewMediaList->setDataRequest(_METHOD_List);
//			connect(this->m_ui->tableViewMediaList, SIGNAL(updated()), this->m_ui->wUpBarMediaList, SLOT(sayReady()));
//			connect(this->m_ui->wUpBarMediaList, SIGNAL(updateClicked()), this, SLOT(refreshMediaList()));

			DeviceMappingActionsDelegage* dmActionDelegage = new DeviceMappingActionsDelegage();
			this->m_ui->tableViewDeviceMapping->getTableView()->setItemDelegate(dmActionDelegage);
			this->m_ui->tableViewDeviceMapping->setMethodList(_MediaZone_METHOD_get_mappings);
			connect(this->m_ui->tableViewDeviceMapping, SIGNAL(updated()), this->m_ui->wUpBarDeviceMapping, SLOT(sayReady()));
			connect(this->m_ui->wUpBarDeviceMapping, SIGNAL(updateClicked()), this, SLOT(refreshDeviceMapping()));


			connect(this->m_ui->tableViewDeviceMapping->getTableModel(), SIGNAL(tableCell_changed(unsigned int,unsigned int, ISerializable*)), this, SLOT(tableViewDeviceMapping_valueChanged(unsigned int,unsigned int, ISerializable*)));

		 }

		virtual ~WidgetMediaZone() {
			delete this->propMapper;
			delete this->m_ui;
		 }

	public slots:
		virtual void refreshAll() {
			this->setWindowTitle( QString("MediaZone %1 [%2]").arg(this->getTargetURI().getName().c_str()).arg(this->getTargetURI().getKernelName().c_str()) );
			this->m_ui->widgetProperties->refreshAll();
			refreshMediaList();
			refreshDeviceMapping();
		 }

	protected slots:
		void refreshMediaList() {
			Request* request_table = new Request(_MediaZone_METHOD_get_medialist);
			postMessageForTarget( new Message(request_table) );
//			this->m_ui->wUpBarMediaList->sayRetrieving();
		 }

		void refreshDeviceMapping() {
			Request* request_table = new Request(_MediaZone_METHOD_get_mappings);
			postMessageForTarget( new Message(request_table) );
			this->m_ui->wUpBarDeviceMapping->sayRetrieving();
		 }

	protected slots:
		void tableViewDeviceMapping_valueChanged( unsigned int row, unsigned int col, ISerializable* newValue )
		 {
			if ( newValue == NULL )
				return;
			(void)row; (void)col;
		 }

	public:
		virtual QWidget* asQWidget()		{ return this; }

		virtual void busMessage( const Message& message, const Bus* ) {
			if ( !message.getContent()->isResponse() ) return;
			if ( !isMessageForMeOrFromTarget(message) )
				return;
			const wosh::Response* response = message.getContent()->asResponse();

			if ( response->getMethod() == _MediaZone_METHOD_get_medialist ) {
				this->m_ui->wWidgetMediaList->loadSerializable( response->getData() );
				return;
			 }

			this->m_ui->tableViewDeviceMapping->evalResponse(response);
	//		this->m_ui->tableViewMediaList->evalResponse(response);

			if ( this->propMapper->evalResponse(response) )
				this->m_ui->wUpBarInfo->sayReady();
		 }

		virtual void setTargetURI( const URI& uri ) {
			IWidget::setTargetURI( uri );
			this->m_ui->widgetProperties->setTargetURI(uri);
			this->m_ui->lblRefURI->setText( QString(uri.toString().c_str()) );
		 }

	protected:
		void changeEvent(QEvent *e) {
			QWidget::changeEvent(e);
			switch (e->type()) {
				case QEvent::LanguageChange:
				m_ui->retranslateUi(this);
				break;
			default: break;
			}
		 }

	protected:
		void buildMenus() {

		 }

	protected:


	protected:
		PropertiesMapper* propMapper;

	private:
		Ui::WidgetMediaZone *m_ui;


}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_WidgetMediaZone_H__
